﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.Entity;
using zichan.DAL;
using MvcPaging;

namespace zichan.BLL
{
    public class MenuService
    {
        public static string AddMenu(Menu model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.Menus.Count(o => o.Action == model.Action && o.Controller == model.Controller && o.Title == model.Title) > 0)
                {
                    return "已存在相同菜单";
                }
                db.Menus.Add(model);
                db.SaveChanges();
                return string.Empty;
            }
        }

        public static bool EditMenu(Menu model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges() > 0;
            }
        }

        public static bool RemoveMenu(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                Menu m = db.Menus.FirstOrDefault(o => o.Id == id);
                if (m != null)
                {
                    db.Menus.Remove(m);
                    db.Database.ExecuteSqlCommand("delete from menus where path like '%,@p0,%'", m.Id);
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public static Menu GetMenu(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.Menus.FirstOrDefault(o => o.Id == id);
            }
        }
        public static IPagedList<Menu> GetMenuList(string name, string controller, string action, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<Menu> w = db.Menus;
                if (!string.IsNullOrEmpty(name))
                    w = w.Where(o => o.Title.Contains(name));
                if (!string.IsNullOrEmpty(controller))
                    w = w.Where(o => o.Controller.Contains(controller));
                if (!string.IsNullOrEmpty(action))
                    w = w.Where(o => o.Action.Contains(action));
                w = w.OrderBy(o => o.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return w.ToList().ToPagedList(pageIndex - 1, pageSize, w.Count());
            }
        }

        public static List<Menu> GetMenuAll()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.Menus.ToList();
            }
        }
        public static List<Menu> GetUserMenus(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<string> ps = (from r in db.Roles join sr in db.StaffRoles on r.Id equals sr.RoleId where sr.StaffId==id select r.Powers).ToList();
                List<int> pids = new List<int>();
                foreach (string p in ps)
                    pids.AddRange(p.Split(new char[] { ','},StringSplitOptions.RemoveEmptyEntries).Select(o=>Convert.ToInt32(o)).ToList());
                pids = pids.Distinct().ToList();
                List<Menu> menus = db.Menus.Where(o => pids.Contains(o.Id)).ToList();
                return menus;
            }
        }

    }
}
